package com.molichuxing.gateway.api.services.impl;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.gateway.api.services.OldUpgradeApiService;
import com.molichuxing.gateway.bff.basic.entity.response.TerminalUpgradedVo;
import com.molichuxing.gateway.bff.basic.entity.response.VersionMobileVo;
import com.molichuxing.gateway.bff.basic.services.VersionBffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 兼容老版本升级接口
 * @author zhaopenghui
 * @date 2019年9月18日
 */
@RestController
@RequestMapping(value = "/webapp/terminal")
@Api(tags = "兼容老版本升级接口")
public class OldUpgradeApiServiceImpl extends RootApiService implements OldUpgradeApiService {
    private static final Logger logger = LoggerFactory.getLogger(OldUpgradeApiServiceImpl.class);

    @Resource
    VersionBffService versionBffService;

    @Override
    @ApiOperation(value = "获取App新版本")
    @GetMapping(value = "/newest")
	public ResponseResult<VersionMobileVo> getNewestMobile(
			@RequestHeader(value = "appVersion", required = true) String appVersion,
			@RequestHeader(value = "channel", required = true) String channel) {
        ResponseResult<VersionMobileVo> result = new ResponseResult<>();
        try {
            result.success(versionBffService.getNewestMobile(appVersion, channel));
            result.setCode(200);
            return result;
        } catch (BizException e) {
            logger.error("[getNewestMobile]获取App新版本异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getNewestMobile]获取App新版本异常", e);
            result.error("获取App新版本异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "获取大屏新版本")
	@GetMapping(value = "/upgrade")
	public ResponseResult<TerminalUpgradedVo> findUpgradeInfo(
			@RequestHeader(value = "appVersion", required = true) String appVersion,
			@RequestHeader(value = "cpuSerial", required = true) String cpuSerial,
			@RequestHeader(value = "chaVersion", required = true) String chaVersion,
			@RequestHeader(value = "opVersion", required = true) String opVersion) {
        ResponseResult<TerminalUpgradedVo> result = new ResponseResult<>();
        try {
            result.success(versionBffService.findUpgradeInfo(appVersion, cpuSerial, chaVersion, opVersion));
            result.setCode(200);
			return result;
        } catch (BizException e) {
            logger.error("[findUpgradeInfo]获取大屏新版本异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[findUpgradeInfo]获取大屏新版本异常", e);
            result.error("获取App新版本异常");
        }
        return result;
    }


}
